<!--公司设置页  -->
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- @close是Dialog 关闭的回调 -->
            <el-dialog :title="formData.id?'编辑人员':'新增人员'" :visible="showDialog" @close="btnCancel">
              <el-form
                ref="addRole"
                :model="formData"
                :rules="rules"
              >
                <el-form-item label="名称" prop="name">
                  <el-input v-model="formData.name" />
                </el-form-item>
                <el-form-item label="描述" prop="description">
                  <el-input v-model="formData.description" />
                </el-form-item>
              </el-form>
              <template #footer>
                <el-button type="primary" size="small" @click="btnOK">确定</el-button>
                <el-button size="small" @click="btnCancel">取消</el-button>
              </template>
            </el-dialog>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <!-- 自定义索引:通过给 type=index 的列传入 index 属性  ; prop:对应列内容的字段名 -->
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignRole(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="edit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoles(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <!-- 有总条数还要有,每页的条数 ,最后elemen会自动算出页数 -->
              <el-pagination layout="prev,pager,next" :total="total" :page-size="pageInfo.pagesize" @current-change="currentChange" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input placeholder="广州回头车信息科技有限公司" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input placeholder="广州市天河区东圃一横路23号" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input placeholder="互联网行业与物流业联动发展优秀服务平台" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
      <el-dialog title="分配权限" :visible="isShowPermission" @close="btnPermCancel">
        <el-checkbox-group v-model="checkList">
          <el-tree :data="permissionList" :props="{label:'name'}" default-expand-all :expand-on-click-node="false">
            <template #default="{data}">
              <!-- 每个属性节点都是一个多选框 -->
              <el-checkbox :label="data.id">
                {{ data.name }}
              </el-checkbox>
            </template>
          </el-tree>
        </el-checkbox-group>
        <template #footer>
          <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRolesList, delRole, addRole, getRolesDetails, updateRole, assignPermissions } from '@/api/company'
import { getPermissionList } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      roleId: '',
      checkList: [],
      permissionList: [],
      companyInfo: {},
      roleList: [],
      pageInfo: {
        page: 1,
        pagesize: 5
      },
      total: 0,
      showDialog: false,
      isShowPermission: false,
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      }
    }
  },
  async created() {
    this.getCompanyInfo()
    this.getRoleList()
    this.getPermissionList()
  },
  methods: {
    // 分配权限确定按钮
    async btnPermOK() {
      // 发送请求
      await assignPermissions({
        id: this.roleId,
        permIds: this.checkList
      })
      // 用户提示
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowPermission = false
    },
    // 分配权限按钮
    async btnPermCancel() {
      // 清空数据
      this.checkList = []
      // 关闭弹窗
      this.isShowPermission = false
    },
    // 分配权限按钮事件
    async assignRole(id) {
      // id点击时候保存起来
      this.roleId = id
      this.isShowPermission = true
      const { permIds } = await getRolesDetails(id)
      this.checkList = permIds
    },
    // 渲染权限列表
    async getPermissionList() {
      this.permissionList = listToTreeData(await getPermissionList(), '0')
    },
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
    },
    async getRoleList() {
      const { rows, total } = await getRolesList(this.pageInfo)
      this.roleList = rows
      this.total = total
      console.log(total)
    },
    currentChange(newPage) {
      this.pageInfo.page = newPage
      this.getRoleList()
    },
    // 删除角色数据
    async delRoles(id) {
      // 弹窗询问
      await this.$confirm('你确认要删除嘛')
      // 发送请求
      await delRole(id)
      // 成功提示
      this.$message.success('删除成功')
      // 重新渲染页面
      this.getRoleList()
    },
    async btnOK() {
      // 校验表单
      await this.$refs.addRole.validate()
      if (this.formData.id) {
        // 编辑
        await updateRole(this.formData)
      } else {
        // 新增
        // 发请求
        await addRole(this.formData)
      }

      // 提示用户
      this.$message.success('新增成功')
      // 更新数据
      this.getRoleList()
      // 关闭弹窗
      this.showDialog = false
    },
    btnCancel() {
      this.$refs.addRole.resetFields()
      this.showDialog = false
      this.formData = {
        name: '',
        description: ''
      }
    },
    async edit(id) {
      this.formData = await getRolesDetails(id)
      this.showDialog = true
    }
  }
}

</script>
<style lang='scss' scoped>
</style>
